const { Op } = require("sequelize");

const contentImageModel = appRequire('models/contentImage');
const thisRequest = require('sync-request');

// 导出对象，供其它模块调用
module.exports = {
    list,
};

async function list(req, request) {
    let postData = req.body
    let returnData = Common.clone(Constant.DEFAULT_SUCCESS);
    let type = 4
    let rids = ""
    let typeId = ""
    let typeName = ""
    let keyWord = ""

    let checkResult = Common.checkUrl(postData, [], request);
    if (checkResult == false) return;

    if (postData.order == "-downLoadNum") type = 3
    if (postData.classDesc == "美术") {
        rids = 3
        typeId = 5
        typeName = "%E7%BE%8E%E6%9C%AF%E7%BB%98%E7%94%BB"
    } else if (postData.classDesc == "书法") {
        rids = 10
        typeId = 4
        typeName = "%E7%BB%83%E5%AD%97%E4%B9%A6%E6%B3%95"
    } else if (postData.classDesc == "招生") {
        rids = 2
        typeId = 8
        typeName = "%E5%AD%A6%E7%A7%91%E8%BE%85%E5%AF%BC"
    }

    let url = "https://poster.miaogeban.com/poster/shareText/list?pageSize=" + postData.pageSize + "&pageNum=" + postData.pageNum + "&rids=" + rids +
        "&typeId=" + typeId + "&typeName=" + typeName + "&type=" + type
    if (postData.searchText) url += "&keyWord=" + encodeURIComponent(postData.searchText)

    var res = thisRequest('GET', url, {
        /*headers: {
            'User-Agent': 'pan.baidu.com'
        }*/
    });
    var body = res.getBody();
    var jsonObj = JSON.parse(body); // 解析接口返回的JSON内容

    returnData.maxCount = jsonObj.data.total
    returnData.maxPage = Math.ceil(jsonObj.data.total / postData.pageSize)
    returnData.data = jsonObj.data.rows
    for (let item of returnData.data) {
        item.content = item.text
        item.images = [item.thumbnail]
        //item.images = ["https://qiniu.jimuzhou.cloud/posterFiles/" + item.tid + "/" + item.tid + ".png-sf"]
    }

    /*let params = {}
    let orders = [['createdAt', 'DESC']]
    if (postData.classDesc) params.classDesc = postData.classDesc
    if (postData.order) {
        if (postData.order.indexOf("-") == -1) {
            orders = [[postData.order, 'ASC']]
        } else {
            orders = [[postData.order.split("-")[1], 'DESC']]
        }
    }
    if (postData.searchText) params.content = { [Op.like]: '%' + postData.searchText + '%' }

    let contentImage = await contentImageModel.findAndCountAll({
        where: params,
        order: orders,
        limit: postData.pageSize,
        offset: postData.pageSize * (postData.pageNum - 1)
    })
    contentImage = Common.clone(contentImage)

    returnData.maxCount = contentImage.count
    returnData.maxPage = Math.ceil(contentImage.count / postData.pageSize)
    returnData.data = contentImage.rows*/
    request.json(returnData)
}